package com.anti.modular.bus.buscostbillhydropower.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.buscostbilldeposit.param.BusCostBillDepositParam;
import com.anti.modular.bus.buscostbillhydropower.entity.BusCostBillHydropower;
import com.anti.modular.bus.buscostbillhydropower.enums.BusCostBillHydropowerExceptionEnum;
import com.anti.modular.bus.buscostbillhydropower.param.BusCostBillHydropowerParam;
import com.anti.modular.bus.buscostbillhydropower.service.BusCostBillHydropowerService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 基础费用账单水电物业表service接口实现类
 *
 * @author anti
 * @date 2021-04-08 10:23:36
 */
@Service
public class BusCostBillHydropowerServiceImpl implements BusCostBillHydropowerService {
    @Resource
    DBUtils dbUtils;

    @Override
    public BusCostBillHydropower getById(BusCostBillHydropowerParam busCostBillHydropowerParam){
        String sql="select * from bus_cost_bill_hydropower where id =? ";
        return dbUtils.get(sql,BusCostBillHydropower.class,busCostBillHydropowerParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,BusCostBillHydropowerParam busCostBillHydropowerParam) {
        StringBuilder sb=new StringBuilder("select * from bus_cost_bill_hydropower where status=0 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(busCostBillHydropowerParam)) {
            // 根据名称 查询
            if (ObjectUtil.isNotEmpty(busCostBillHydropowerParam.getName())) {
               sb.append(" and name = ?");
               param.add(busCostBillHydropowerParam.getName());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<BusCostBillHydropower> list(BusCostBillHydropowerParam busCostBillHydropowerParam) {
        String sql="select * from bus_cost_bill_hydropower where status=0";
        return dbUtils.query(sql,BusCostBillHydropower.class);
    }

    @Override
    public void add(BusCostBillHydropowerParam busCostBillHydropowerParam) {
        checkParam(busCostBillHydropowerParam,false);
        BusCostBillHydropower busCostBillHydropower = new BusCostBillHydropower();
        BeanUtil.copyProperties(busCostBillHydropowerParam, busCostBillHydropower);
        busCostBillHydropower.setStatus(0);
        busCostBillHydropower.setCreateTime(new Date());
        busCostBillHydropower.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(busCostBillHydropower);
    }


    public void delete(BusCostBillHydropowerParam busCostBillHydropowerParam) {
        BusCostBillHydropower busCostBillHydropower = this.queryBusCostBillHydropower(busCostBillHydropowerParam);
        busCostBillHydropower.setUpdateTime(new Date());
        busCostBillHydropower.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        busCostBillHydropower.setStatus(2);
        dbUtils.update(busCostBillHydropower);
    }

    public void edit(BusCostBillHydropowerParam busCostBillHydropowerParam) {
        checkParam(busCostBillHydropowerParam,true);
        BusCostBillHydropower busCostBillHydropower = this.queryBusCostBillHydropower(busCostBillHydropowerParam);
        BeanUtil.copyProperties(busCostBillHydropowerParam, busCostBillHydropower);
        busCostBillHydropower.setUpdateTime(new Date());
        busCostBillHydropower.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(busCostBillHydropower);
    }

    @Override
    public BusCostBillHydropower detail(BusCostBillHydropowerParam busCostBillHydropowerParam) {
        return this.queryBusCostBillHydropower(busCostBillHydropowerParam);
    }

    /**
     * 获取基础费用账单水电物业表
     *
     * @author anti
     * @date 2021-04-08 10:23:36
     */
    private BusCostBillHydropower queryBusCostBillHydropower(BusCostBillHydropowerParam busCostBillHydropowerParam) {
        BusCostBillHydropower busCostBillHydropower = this.getById(busCostBillHydropowerParam);
        if (ObjectUtil.isNull(busCostBillHydropower)) {
            throw new BusinessException(BusCostBillHydropowerExceptionEnum.NOT_EXIST);
        }
        return busCostBillHydropower;
    }

    private void checkParam(BusCostBillHydropowerParam busCostBillHydropowerParam, boolean isExcludeSelf){
        Long id = busCostBillHydropowerParam.getId();
        String depositName = busCostBillHydropowerParam.getName();
        //构建带name查询条件
        StringBuilder nameSb=new StringBuilder("select count(*) from bus_cost_bill_hydropower where status =0 and name =?");
        ArrayList<Object> nameParams = CollectionUtil.newArrayList();
        nameParams.add(depositName);
        //如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            nameSb.append(" and id <> ?");
            nameParams.add(id);
        }
        //查询重复记录的数量
        int countByName = dbUtils.getIntValue(nameSb.toString(),nameParams.toArray());

        //如果存在重复的记录，抛出异常，直接返回前端
        if (countByName >= 1) {
            throw new BusinessException(0,"該水電物業已存在");
        }
    }
}
